package cn.csust.net2.manager.client.app;

import cn.csust.net2.manager.client.util.ClassTools;
import cn.csust.net2.manager.client.ux.AuthorityEvent;
import cn.csust.net2.manager.client.ux.AuthorityEventHandler;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;

public class TabSetPanel extends TabPanel implements AppPanel,
		AuthorityEventHandler {
	protected App app;

	public TabSetPanel(App app) {
		this.app = app;
		// 将该类纳入事件管理器
		app.eventBus.addHandler(AuthorityEvent.TYPE, this);
		init();

	}

	/**
	 * 初始化
	 */
	private void init() {
		// 初始化渲染信息
		this.setMinTabWidth(100);
		this.setResizeTabs(true);
		this.setAnimScroll(true);
		this.setTabScroll(true);
		this.setCloseContextMenu(true);
	}

	@Override
	public BorderLayoutData getLayoutInfo() {
		BorderLayoutData layoutData = new BorderLayoutData(LayoutRegion.CENTER);
		layoutData.setMargins(new Margins(0));
		return layoutData;
	}

	public static final String BASE_CLASS_URL = "cn.csust.net2.manager.client.module.";

	/**
	 *  当有权限信息抛出时候，改方法被触发
	 */
	@Override
	public void load(AuthorityEvent authorityEvent) {
		if (this.getItemByItemId(authorityEvent.getAuthority().getDisplayName()) == null) {
			long start = System.currentTimeMillis();
			System.out.println("系统加载模块："
					+ authorityEvent.getAuthority().getAuthorityName());
			TabItem tabItem = ClassTools.newInstance(TabSetPanel.BASE_CLASS_URL
					+ authorityEvent.getAuthority().getAuthorityName());
			this.add(tabItem);
			long end = System.currentTimeMillis();
			System.out.println("反射的时间消耗为:" + (end - start));
		}
		this.setSelection(this.getItemByItemId(authorityEvent.getAuthority()
				.getDisplayName()));
	}
}
